var API            = require('API')

module.exports = ['$scope', function ($scope) {

    $scope.shop_all = [];
    

    


    //获取所有门店信息
    $scope.shop_all_min = [];
    $scope.shop_all_max = [];

    function get_shops() {
        API.shop.all(function (data) {
            $scope.$apply(function () {
                console.log(data);
                $scope.shop_all = data;
                for(var i=0;i<data.length;i++){
                    if (data[i].seq <= 0) {
                        var min = {};
                        min.name = data[i].name;
                        min.id = data[i].id;
                        $scope.shop_all_min.push(min);
                    }else{
                        var max ={};
                        max.name = data[i].name;
                        max.id = data[i].id;
                        $scope.shop_all_max.push(max);
                    }
                }
            });
        });
    };
    get_shops();

    $scope.first_min = true;
    $scope.is_last_min = function(index){
        if(index == $scope.shop_all_min.length - 1){
            if($scope.first_min){
                $scope.first_min = false;
                $(".shop-sorter-min").shapeshift();
            }
        }
    };
    $scope.first_max = true;
    $scope.is_last_max = function(index){
        if(index == $scope.shop_all_max.length - 1){
            if($scope.first_max){
                $scope.first_max = false;
                $(".shop-sorter-max").shapeshift();
            }
        }
    };

    $('.shop-sorter-container').on('ss-drop-complete',function() {
        var param ={};
        var str_min = '';
        var str_max = '';

        for(var i = 0;i < $(".shop-sorter-min div").length;i++){
            str_min = $(".shop-sorter-min div").eq(i).attr("value") + ';' + str_min;
        };
        for(var i = 0;i < $(".shop-sorter-max div").length;i++){
            str_max = str_max + ';' + $(".shop-sorter-max div").eq(i).attr("value");
        };

        param.disable_shops = str_min.substring(1);
        param.sorted_shops = str_max.substring(1);

        API.shop.sort(param,function(data){
            console.log('max'+data);
        });

    });


}];
